package com.pb.b2b.bean;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.pb.b2b.model.Language;
import com.pb.b2b.service.LanguageService;
import com.pb.b2b.util.FacesUtils;

@Component
@Scope("view")
@ManagedBean(name = "createLanguageBean")
public class CreateLanguageBean extends BaseBean {

	@Autowired
	LanguageService languageService;

	private String name;
	private String code;

	@PostConstruct
	public void init() {

	}

	/* METHOD */
	@Override
	public String confirmAction() {
		if (StringUtils.isEmpty(name)) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR, "ERROR_LANGUAGE_NAME_CAN_NOT_EMPTY");
			return RETURN_ERROR;
		}

		if (StringUtils.isEmpty(code)) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR, "ERROR_LANGUAGE_CODE_CAN_NOT_EMPTY");
			return RETURN_ERROR;
		}
		return RETURN_SUCCESS;
	}

	@Override
	public String executeAction() {
		Language language = new Language();
		language.setName(name);
		language.setCode(code);
		languageService.createLanguage(language);
		if (hasError()) {
			return RETURN_ERROR;
		}
		return RETURN_SUCCESS;
	}

	/* GETTER - SETTER */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

}
